/**--------------------------------------------------------------------------
 * Author: lvzeyuan@126.com
 * Date: 2017.10.10
 * Description: 站点相关全局配置及公用函数文件
 *              
 * History:
 -------------------------------------------------------------------------*/


//#region ---------------------------------------------------------全局配置

var SiteConfig={};

SiteConfig.webApi='http://iot.hzcando.com/api/idm/v1/';

SiteConfig.chartColors=['#FF3D50', '#FFC526', '#03BAFB', '#12E266','#00E7D4'];

SiteConfig.account={
    CompanyLogo:'',
    CompanyName:'',
    id:'',//当前登录用户ID
    systemTitle:'',
    accesstoken:'', //访问令牌
    expiresIn:0, //失效时间
    refreshtoken:'',
    roleId:'',
    uId:'', //全局唯一标识
    loginTime:'',//登录时间
    userName:''
};


SiteConfig.menu = [
     {
         name:'test',
         index:'1',
         url:'pages/compy/test.html'
     },
     {
        name: '企业申报进度',
        index: '3',
        icon: 'icon-monitor',
        child: [
            /*
            {
                name: '企业申报进度汇总表',
                index:'3-1',
                url:''
            },*/
            {
                name: '企业申报进度明细表',
                index:'3-2',
                url:'pages/gover/declare/pross-list.html'
            }
        ]
     }/*, {
        name: '企业管理员信息',
        index: '4',
        icon: 'icon-ele-ana',
        child:[
            {
                name:'重点用能单位信息表',
                index:'4-1',
                url:''
            },{
                name:'能管员信息查询',
                index:'4-2',
                url:''
            }
        ]
    }*/,
    {
        name: '月报审核',
        index: '6',
        icon: 'icon-network',
        child:[
            {
                name:'月报审核',
                index:'6-1',
                url:''
            }/*,{
                name:'月报审核日志',
                index:'6-2',
                url:''
            }*/
        ]
    },{
        name: '月报报表',
        index: '7',
        icon: 'icon-report',
        child: [
            /*
            {
                name: '企业能耗排行榜',
                url: '',
                index: '7-1'
            },*/
            {
                name: '能源消费情况汇总表',
                url: 'pages/gover/report/consume-count.html',
                index: '7-2'
            },
            {
                name: '单耗汇总表',
                url: 'pages/gover/report/single-consume-count.html',
                index: '7-3'
            }/*,
            {
                name: '单耗分析',
                url: '',
                index: '7-4'
            },
            {
                name: '企业单耗指标项管理',
                url: '',
                index: '7-5'
            }*/,
            {
                name: '产值能耗汇总表',
                url: 'pages/gover/report/value-consume-count.html',
                index: '7-6'
            }/*,
            {
                name: '汇总分析',
                url: '',
                index: '7-7'
            },{
                name: '新单耗分析',
                url: '',
                index: '7-8'
            }*/
        ]
    }/*,
    {
        name: '年报报表',
        index: '8',
        icon: 'icon-opera',
        child: [
            {
                name: '节能目标完成情况表',
                url: '',
                index: '8-1'
            },
            {
                name: '节能技改情况汇总表',
                url: '',
                index: '8-2'
            },
            {
                name: '工艺生产线汇总表',
                url: '',
                index: '8-3'
            },
            {
                name: '主要用热设备汇总表',
                url: '',
                index: '8-4'
            },{
                name: '主要用电设备汇总表',
                url: '',
                index: '8-5'
            },
            {
                name: '主要用水情况汇总表',
                url: '',
                index: '8-6'
            }
        ]
    },
    {
        name: '图形化报表',
        index: '9',
        icon: 'icon-dev',
        child: [
            {
                name: '能源消费情况图表',
                url: '',
                index: '9-1'
            },
            {
                name: '产品单耗汇总图表',
                url: '',
                index: '9-2'
            },
            {
                name: '产值能耗汇总图表',
                url: '',
                index: '9-3'
            },{
                name: '浙江省总能耗变化率',
                url: '',
                index: '9-4'
            },{
                name: '企业分布情况',
                url: '',
                index: '9-5'
            }
        ]
    },{
        name:'异常数据分析',
        index: '10',
        icon: 'icon-dev',
        child:[
            {
                name: '能源消费量异常',
                url: '',
                index: '10-1'
            }
        ]
    }*/
];

//百度地图样式
SiteConfig.mapStyle={
    styleJson: [
            {
                "featureType": "water",
                "elementType": "all",
                "stylers": {
                    "color": "#00141E"
                }
            },
            {
                "featureType": "land",
                "elementType": "all",
                "stylers": {
                    "color": "#00263A"
                }
            },
            {
                "featureType": "boundary",
                "elementType": "geometry",
                "stylers": {
                    "color": "#064f85"
                }
            },
            {
                "featureType": "railway",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "highway",
                "elementType": "geometry",
                "stylers": {
                    "color": "#004981"
                }
            },
            {
                "featureType": "highway",
                "elementType": "geometry.fill",
                "stylers": {
                    "color": "#005b96",
                    "lightness": 1
                }
            },
            {
                "featureType": "highway",
                "elementType": "labels",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "arterial",
                "elementType": "geometry",
                "stylers": {
                    "color": "#004981"
                }
            },
            {
                "featureType": "arterial",
                "elementType": "geometry.fill",
                "stylers": {
                    "color": "#00508b"
                }
            },
            {
                "featureType": "poi",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "green",
                "elementType": "all",
                "stylers": {
                    "color": "#056197",
                    "visibility": "off"
                }
            },
            {
                "featureType": "subway",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "manmade",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "local",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "arterial",
                "elementType": "labels",
                "stylers": {
                    "visibility": "off"
                }
            },
            {
                "featureType": "boundary",
                "elementType": "geometry.fill",
                "stylers": {
                    "color": "#029fd4"
                }
            },
            {
                "featureType": "building",
                "elementType": "all",
                "stylers": {
                    "color": "#1a5787"
                }
            },
            {
                "featureType": "label",
                "elementType": "all",
                "stylers": {
                    "visibility": "off"
                }
            }
    ]
};


//从缓存中恢复账户信息
var userAccount = localStorage.getItem('user-account');
if(userAccount){
    SiteConfig.account=JSON.parse(userAccount);
};

//从缓存中恢复菜单
var userMenu = localStorage.getItem('user-menu');
if(userMenu){
    SiteConfig.menu=JSON.parse(userMenu);
};

//#endregion



//#region ---------------------------------------------------------公用函数，其它服务端api调用写在各自页面内

//判断用户是否已经登录
function isLogined(){
    if(SiteConfig.account.uId &&
        ( new Date().getTime()- new Date(SiteConfig.account.loginTime).getTime() )  < SiteConfig.account.expiresIn ){

           return true;
    }
    return false;
}

function userLogin(username, password){
    if(this.isLogined() ){

        return {
            "success":function(callback){
                callback({state:0});
            }
        };
    }

    return {
        "success":function(callback){
            HttpUtil.get('oauth2/accessToken', {
                client_id: username,
                client_secret: password,
                scope: 'read,write'
            }).success(function(resData){
                var data = resData.data;
                SiteConfig.account={
                    companyLogo:data.CompanyLogo,
                    companyName:data.CompanyName,
                    id:data.Id,
                    systemTitle:data.SystemTitle,
                    accessToken:data.access_token,
                    expiresIn: data.expires_in*1000, //毫秒
                    refreshToken: data.refresh_token,
                    roleId: data.roleId,
                    uId: data.uid, //当前会话token对应的唯一
                    loginTime:TimeUtil.convertToString(new Date()),
                    userName:username
                };

                console.log('SiteConfig.account',SiteConfig.account);

                localStorage.setItem('user-account', JSON.stringify(SiteConfig.account) );

                /*
                //获取用户菜单
                HttpUtil.get('oauth2/getUserMenu', null)
                .success(function(menuData){
                    SiteConfig.menu = menuData;
                    localStorage.setItem('user-menu', JSON.stringify(SiteConfig.menu) );
                    callback({state:0});
                });*/

                callback({state:0});
            });
        }
    };
}

function logout(){
    localStorage.removeItem('user-account');
    return {
        "success":function(callback){
            callback();
        }
    };
}



//#endregion

